package creation_patterns.singleton_pattern.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ContainerRegister {
    private static Map<String, Object> objectMap = new ConcurrentHashMap();

    public Object getInstance(String className) {
        // spring框架中锁的就是IOC容器
        synchronized (objectMap) {
            if (!objectMap.containsKey(className)) {
                Object o = null;
                try {
                    o = Class.forName(className).newInstance();
                    objectMap.put(className, o);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return objectMap.get(className);
    }
}
